﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.SpecialUtils.ContentRecognition.BaiduUtils.Ocr;
using IDCardHelper.UserUtils.Ocr;
using IDCardHelper.UserUtils.Ocr.OcrJson;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDCardHelper.UserUtils
{
    internal class OcrHelper
    {
        /// <summary>
        /// 身份证正面
        /// </summary>
        public static void IdCardFront()
        {
            string ImgFilePath = FileHelper.SelectFile();
            if (ImgFilePath == "")
                return;
            string Content = BaiduOcrhelper.ReadIDCardFile(ImgFilePath, "front");
            IDCasrdSaveInfo(Content, ImgFilePath);
        }

        /// <summary>
        ///保存相关信息
        /// </summary>
        /// <param name="content"></param>
        private static void IDCasrdSaveInfo(string content, string ImgInfoPath)
        {
            string TempContent = "";
            try
            {
                IdCardJson.Root IDValue = JsonConvert.DeserializeObject<IdCardJson.Root>(content);

                #region 依次判断写入结果

                if (IDValue.words_result.住址 != null)
                {
                    TempContent += $"\r\n住址：{IDValue.words_result.住址.words}";
                }
                if (IDValue.words_result.公民身份号码 != null)
                {
                    TempContent += $"\r\n公民身份号码：{IDValue.words_result.公民身份号码.words}";
                }
                if (IDValue.words_result.出生 != null)
                {
                    TempContent += $"\r\n出生：{IDValue.words_result.出生.words}";
                }
                if (IDValue.words_result.失效日期 != null)
                {
                    TempContent += $"\r\n失效日期：{IDValue.words_result.失效日期.words}";
                }
                if (IDValue.words_result.姓名 != null)
                {
                    TempContent += $"\r\n姓名：{IDValue.words_result.姓名.words}";
                }
                if (IDValue.words_result.性别 != null)
                {
                    TempContent += $"\r\n性别：{IDValue.words_result.性别.words}";
                }
                if (IDValue.words_result.民族 != null)
                {
                    TempContent += $"\r\n民族：{IDValue.words_result.民族.words}";
                }
                if (IDValue.words_result.签发日期 != null)
                {
                    TempContent += $"\r\n签发日期：{IDValue.words_result.签发日期.words}";
                }
                if (IDValue.words_result.签发机关 != null)
                {
                    TempContent += $"\r\n签发机关：{IDValue.words_result.签发机关.words}";
                }

                #endregion 依次判断写入结果
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            FileHelper.AppendText(FloderNameHelper.GetFloderPath(ImgInfoPath) + "身份证信息.txt", TempContent);
        }

        /// <summary>
        /// 身份证反面
        /// </summary>
        public static void IdCardBack()
        {
            string ImgFilePath = FileHelper.SelectFile();
            if (ImgFilePath == "")
                return;
            string Content = BaiduOcrhelper.ReadIDCardFile(ImgFilePath, "back");
            IDCasrdSaveInfo(Content, ImgFilePath);
        }

        /// <summary>
        ///处理银行卡
        /// </summary>
        public static void BankCard()
        {
            string ImgFilePath = FileHelper.SelectFile();
            if (ImgFilePath == "")
                return;
            string Content = BaiduOcrhelper.ReadBankCard(ImgFilePath);
            //   SaveBusinessLicense(Content, ImgFilePath);
            SaveBankCard(Content, ImgFilePath);
        }

        /// <summary>
        /// 保存银行卡信息
        /// </summary>
        /// <param name="content"></param>
        /// <param name="ImgInfoPath"></param>
        private static void SaveBankCard(string content, string ImgInfoPath)
        {
            string TempContent = "";
            try
            {
                BankCardJson.Root BusinessLicense = JsonConvert.DeserializeObject<BankCardJson.Root>(content);

                #region 依次判断写入结果

                if (BusinessLicense.result.bank_card_number != null)
                {
                    TempContent += $"\r\n银行卡卡号：{BusinessLicense.result.bank_card_number.Replace(" ", "")}";
                }
                if (BusinessLicense.result.bank_name != null)
                {
                    TempContent += $"\r\n银行名：{BusinessLicense.result.bank_name}";
                    TempContent += AddBankID(BusinessLicense.result.bank_name);
                }
                if (BusinessLicense.result.bank_card_number != null)
                {
                    TempContent += $"\r\n失效时间：{BusinessLicense.result.valid_date}";
                }

                #endregion 依次判断写入结果
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            FileHelper.AppendText(FloderNameHelper.GetFloderPath(ImgInfoPath) + "身份证信息.txt", TempContent);
        }

        private static string AddBankID(string bank_name)
        {
            string TempBankID = "";
            if (bank_name.IndexOf("工商") >= 0)
            {
                TempBankID = "102475500807";
            }
            else if (bank_name.IndexOf("农业") >= 0)
            {
                TempBankID = "103475591915";
            }
            else if (bank_name.IndexOf("建设") >= 0)
            {
                TempBankID = "105475501307";
            }
            else if (bank_name.IndexOf("信用社") >= 0)
            {
                TempBankID = "402475501006";
            }
            else if (bank_name.IndexOf("中银") >= 0)
            {
                TempBankID = "320475500013";
            }
            else if (bank_name.IndexOf("莱商") >= 0)
            {
                TempBankID = "313475500649";
            }
            else if (bank_name.IndexOf("济宁") >= 0)
            {
                TempBankID = "313475002327";
            }
            else if (bank_name.IndexOf("邮政") >= 0)
            {
                TempBankID = "403475500229";
            }
            else if (bank_name.IndexOf("邮储银行") >= 0)
            {
                TempBankID = "403475500229";
            }

            if (TempBankID != "")
                return $"联行号：{TempBankID}";
            else
                return $"未查询到联行号，银行名称:{bank_name}";
        }

        /// <summary>
        ///营业执照
        /// </summary>
        public static void BusinessLicense()
        {
            string ImgFilePath = FileHelper.SelectFile();
            if (ImgFilePath == "")
                return;
            string Content = BaiduOcrhelper.ReadBusinessLicense(ImgFilePath);
            SaveBusinessLicense(Content, ImgFilePath);
        }

        /// <summary>
        /// 保存营业执照
        /// </summary>
        /// <param name="content"></param>
        /// <param name="ImgInfoPath"></param>
        private static void SaveBusinessLicense(string content, string ImgInfoPath)
        {
            string TempContent = "";
            try
            {
                BusinessLicenseJson.Root BusinessLicense = JsonConvert.DeserializeObject<BusinessLicenseJson.Root>(content);

                #region 依次判断写入结果

                if (BusinessLicense.words_result.单位名称 != null)
                {
                    TempContent += $"\r\n单位名称：{BusinessLicense.words_result.单位名称.words}";
                }
                if (BusinessLicense.words_result.地址 != null)
                {
                    TempContent += $"\r\n地址：{BusinessLicense.words_result.地址.words}";
                }
                if (BusinessLicense.words_result.成立日期 != null)
                {
                    TempContent += $"\r\n成立日期：{BusinessLicense.words_result.成立日期.words}";
                }
                if (BusinessLicense.words_result.有效期 != null)
                {
                    TempContent += $"\r\n有效期：{BusinessLicense.words_result.有效期.words}";
                }
                if (BusinessLicense.words_result.法人 != null)
                {
                    TempContent += $"\r\n法人：{BusinessLicense.words_result.法人.words}";
                }
                if (BusinessLicense.words_result.注册资本 != null)
                {
                    TempContent += $"\r\n注册资本：{BusinessLicense.words_result.注册资本.words}";
                }
                if (BusinessLicense.words_result.社会信用代码 != null)
                {
                    TempContent += $"\r\n社会信用代码：{BusinessLicense.words_result.社会信用代码.words}";
                }
                if (BusinessLicense.words_result.类型 != null)
                {
                    TempContent += $"\r\n类型：{BusinessLicense.words_result.类型.words}";
                }
                if (BusinessLicense.words_result.组成形式 != null)
                {
                    TempContent += $"\r\n组成形式：{BusinessLicense.words_result.组成形式.words}";
                }
                if (BusinessLicense.words_result.组成形式 != null)
                {
                    TempContent += $"\r\n经营范围：{BusinessLicense.words_result.经营范围.words}";
                }
                if (BusinessLicense.words_result.证件编号 != null)
                {
                    TempContent += $"\r\n组成形式：{BusinessLicense.words_result.组成形式.words}";
                }
                if (BusinessLicense.words_result.组成形式 != null)
                {
                    TempContent += $"\r\n证件编号：{BusinessLicense.words_result.证件编号.words}";
                }

                #endregion 依次判断写入结果
            }
            catch (Exception ex)
            {
                PrintLog.E(ex);
            }
            FileHelper.AppendText(FloderNameHelper.GetFloderPath(ImgInfoPath) + "身份证信息.txt", TempContent);
        }

        /// <summary>
        /// 阅读小票
        /// </summary>
        /// <returns></returns>
        public static void ReadReceipt()
        {
            string FilePath = FileHelper.SelectFile();
            if (FilePath == "")
                return;
            string Content = BaiduOcrhelper.ReadReceipt(FilePath);
            ReadReceipt(Content, FilePath);
        }

        /// <summary>
        /// 阅读小票
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="ImgInfoPath"></param>
        /// <returns></returns>
        public static string ReadReceipt(string Content, string ImgInfoPath)
        {
            ReceiptJson.Root root = JsonConvert.DeserializeObject<ReceiptJson.Root>(Content);
            if (root == null )
            {
                PrintLog.E("格式化JSON失败，返回值如下：\r\n" + Content);
            }
            Console.WriteLine(root.words_result_num);
            string FileContent = "";
            foreach (var item in root.words_result)
            {
                FileContent += "\r\n" + item.words;
            }
            string File = ".\\小票识别.txt";
            FileHelper.WriteText(File, FileContent);
            FloderHelper.OpenFloder(File);
            return "";
        }
    }
}